<?php

/**
 * @author Aurelijus Banelis
 */
class ForeseeDesign {
    /**
     *
     * @param array $subdivisions   [code]=>name
     * @param array $dates          [#]=>dade
     */
    public static function show_input($subdivisions, $dates, $default_values = array()) {
        ?>
        <form class="new_measure" action="#" method="post">
            <?
            $unique_index = 0;
            $n = 0;
            if (array_key_exists('new_measures', $default_values)) {
                $n = count(self::get_array_value($default_values, 'new_measure'));
            }
            if ($n < 2) {
                $n = 2;
            }
            for($id=0; $id<$n; $id++) {
                $name = self::get_array_value($default_values, 'new_measure', "n$id");
                ?>
                <div class="new_measure" id="new_measure_n<?=$id;?>">
                    <div class="measure_title">
                        Pavadinimas: <input name="new_measure[n<?=$id;?>]" value="<?=$name;?>"/>
                    </div>
                    <div class="measure_subdvision">
                        <b>Administravimo laikas:</b><br/>
                        <?
                            foreach ($subdivisions as $code=>$name) {
                                $code = htmlspecialchars($code);
                                $name = htmlspecialchars($name);
                                $count = self::get_array_value($default_values, 'subdivision', "n$id", $code);
                                echo "<label for='sub$unique_index'>$name</label>: " .
                                     "<input name='subdivision[n$id][$code]' id='sub$unique_index' value='$count'/><br/>";
                                $unique_index++;
                            }
                        ?>
                    </div>
                    <div class="measure_quantities">
                        <b>Numatomi paraiškų kiekiai</b> (gauta ir suadministruota)<br/>
                        <?
                            $first = true;
                            foreach ($dates as $date) {
                                $date = htmlspecialchars($date);
                                $received = self::get_array_value($default_values, 'received', "n$id", $date);
                                $administrated = self::get_array_value($default_values, 'administrated', "n$id", $date);
                                echo "$date: <input title='Gauta' name='received[n$id][$date]'  value='$received'/><input title='Suadministruota' name='administrated[n$id][$date]' value='$administrated'/><br/>";
                                if ($first) {
                                    $first = false;
                                    ?><a href="javascript:fill_foresee_quantities('new_measure_n<?=$id;?>')" class="autofill">Užpildyti tuščius laukelius</a><br/><?
                                }
                            }
                        ?>
                    </div>
                </div>
                <?
            }
            ?>
            <input type="reset" value="Atstatyti pradines reikšmes"/>
            <input type="submit" value="Išsaugoti ir atnaujinti diagramas ir lenteles"/>
        </form>
        <?
    }
    
    public static function get_array_value($array, $key, $key2 = null, $key3 = null) {
        if ($key2 === null && array_key_exists($key, $array)) {
            return $array[$key];
        } else if ($key3 === null && array_key_exists($key, $array) && array_key_exists($key2, $array[$key])) {
            return $array[$key][$key2];
        } else if (array_key_exists($key, $array) && array_key_exists($key2, $array[$key]) && array_key_exists($key3, $array[$key][$key2])) {
            return $array[$key][$key2][$key3];
        } else {
            return '';
        }
    }
}
